package com.ap.core.jbehave.manageconsumer;

import com.ap.core.cxf.jaxb.common.ActivationStatus;
import com.ap.core.cxf.jaxb.common.RequestTypeCode;
import com.ap.core.cxf.jaxb.common.UpdateStatus;
import com.ap.core.cxf.jaxb.manageconsumer.RequestRegisterConsumerType;
import com.ap.core.cxf.jaxb.manageconsumer.ResponseRegisterConsumerType;
import com.ap.core.cxf.jaxb.msghdr.MessageType;
import com.ap.core.jbehave.BaseRTPSteps;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.generator.registerconsumer.RequestManageConsumerGeneratorBuilder;
import com.core.ap.wsdl.manageconsumerservice_1.ManageConsumer1PortType;
import com.core.ap.wsdl.manageconsumerservice_1.RegisterConsumer1Fault;
import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class ManageConsumerSteps extends BaseRTPSteps {

    private RequestRegisterConsumerType requestRegisterConsumerType;
    private ResponseRegisterConsumerType responseRegisterConsumerType;
    /**
     * @param storyContext
     */
    public ManageConsumerSteps(StoryContext storyContext) {
        super(storyContext);
    }

    @Given("Consumer requested to register for ZAPP with FI via mobile app")
    public void givenConsumerRegisteredToZapp() {
    }

    @Given("FI will send MessageType as REGISTERCONSUMER")
    public void givenFISendMessageAsREGISTERCONSUMER() {
    }

    @Given("RequestType as REGISTER and with valid FI Id, unique Consumer id, email id, password, mobile number")
    public void givenWithRequestTypeAsREGISTER() throws IOException {
        requestRegisterConsumerType = RequestManageConsumerGeneratorBuilder.getRequestManageConsumerTypeGenerator().build().generate();
        requestRegisterConsumerType.getRegistrationInfo().setFIId("000001");
        requestRegisterConsumerType.getRegistrationInfo().setMobNb("+4407535434437");
        requestRegisterConsumerType.getRegistrationInfo().setRequestType(RequestTypeCode.REGISTER);
        requestRegisterConsumerType.getRegistrationInfo().getCredentials().setEmailId(requestRegisterConsumerType.getRegistrationInfo().getCredentials().getEmailId() + "@gmail.com");
        JaxbUtil.newInstance().javaToXML(RequestRegisterConsumerType.class, requestRegisterConsumerType);
    }


    @When("Consumer's FI sends Consumer registration message to AP Service")
    public void whenConsumersFISendsConsumerRegistrationmessagetoAPService() throws RegisterConsumer1Fault {
        final ManageConsumer1PortType manageConsumer1PortType = factory.getBean(ManageConsumer1PortType.class);
        responseRegisterConsumerType = manageConsumer1PortType.registerConsumer1(requestRegisterConsumerType);
        JaxbUtil.newInstance().javaToXML(ResponseRegisterConsumerType.class, responseRegisterConsumerType);
    }

    @Then("AP Service will respond with FI Id, FI Consumer Id,ConsumerPortalUrl")
    public void thenAPServiceWillRespondwithFIIdFIConsumerIdConsumerPortalUrl() {
        assertThat("Message Type  is not as expected", true, CoreMatchers.is(equalTo(responseRegisterConsumerType.getMsgHdr().getMsgType() == MessageType.ACKCONSREGISTRATION)));
        assertThat("Original message Id  is not as expected", true, CoreMatchers.is(equalTo(responseRegisterConsumerType.getMsgHdr().getOrgReqMsgId().equals(requestRegisterConsumerType.getMsgHdr().getMsgId()))));
    }

    @Then("UpdateStatus as SUCCESSFUL and ActivationStatus as REGISTERED")
    public void thenUpdateStatusAsSUCCESSFULAndActivationStatusAsREGISTERED() {
        assertThat("Activation Status  is not as expected", true, CoreMatchers.is(equalTo(responseRegisterConsumerType.getConsumerRegistResponse().getActivationStatus() == ActivationStatus.REGISTERED)));
        assertThat("Update Status  is not as expected", true, CoreMatchers.is(equalTo(responseRegisterConsumerType.getConsumerRegistResponse().getUpdateStatus() == UpdateStatus.SUCCESSFUL)));

    }

}
